<template>
	<div class="allcontent">
		<div class="search">
			<el-form @submit.native.prevent :model="searchParam" ref="queryFormRef">
				<el-row gutter="10">
					<el-col span="8">
						<el-form-item prop="userName">
							<el-input  size="default"   @keyup.enter.native="search" placeholder="请输入账号模糊搜索" autofocus v-model="searchParam.userName"></el-input>
						</el-form-item>
					</el-col>
					<el-col  span="8">
						<el-form-item prop="phone">
							<el-input  size="default"   @keyup.enter.native="search" placeholder="请输入手机号模糊搜索"  v-model="searchParam.phone"></el-input>
						</el-form-item>
					</el-col>
					<el-col  span="8">
						<el-button round  size="default"  plain icon="Search" type="primary" @click="search">搜索</el-button>
					</el-col>
					<el-col  span="8">
						<el-button round  size="default"  plain icon="Refresh" type="warning" @click="reset(queryFormRef)">重置</el-button>
					</el-col>
				</el-row>
				<el-row gutter="10" style="margin-top:10px;margin-bottom: 10px;">
					<el-col span="8"  v-if="userStore.hasPermission('system:role:assignUsers')" >
						<el-button round  size="default"  plain icon="Edit" type="success" @click="addAssignUser()">添加用户</el-button>
					</el-col>
					<el-col span="8"   v-if="userStore.hasPermission('system:role:unAssignUsers')" >
						<el-popconfirm
						    confirm-button-text="是"
						    cancel-button-text="否"
						    :icon="InfoFilled"
						    icon-color="#626AEF"
						    title="是否取消授权该数据?"
						    @confirm="cancelAssignUser()"
						  >
						    <template #reference>
						      <el-button round  size="default" icon="Delete"  plain  type="danger"
							:disabled="selections.length === 0">
								  取消授权
						      </el-button>
						    </template>
						</el-popconfirm>
					</el-col>
					<el-col span="8">
            <el-button round  size="default"  plain icon="Close" type="warning"  @click="close">关闭</el-button>
					</el-col>
				</el-row>
			</el-form>
		</div>
		<div class="el-table">
			<el-table :data="tableData" style="width: 100%;" v-loading="loading" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55"></el-table-column>
			    <el-table-column prop="userName" label="账号" width="180" />
			    <el-table-column prop="nickName" label="昵称" width="180" />
			    <el-table-column prop="email" label="邮箱" />
				<el-table-column prop="phone" label="手机号" />
				<el-table-column prop="status" label="状态">
					<template  #default="scope">
						<el-tag v-if="scope.row.status == 1">正常</el-tag>
						<el-tag type="danger" v-else-if="scope.row.status == 0">禁用</el-tag>
					</template>
				</el-table-column>	
				<el-table-column  label="操作">
					<template  #default="scope">
						<el-popconfirm   v-if="userStore.hasPermission('system:role:unAssignUsers')" 
						    confirm-button-text="是"
						    cancel-button-text="否"
						    :icon="InfoFilled"
						    icon-color="#626AEF"
						    title="是否取消授权该数据?"
						    @confirm="cancelAssignUser(scope.row.userRoleId)"
						  >
						    <template #reference>
						      <el-button round  plain  size="default" icon="Delete" type="danger">
								  取消授权
						      </el-button>
						    </template>
						  </el-popconfirm>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<div class="el-pager">
		   <el-pagination layout="total,sizes,prev, pager, next,jumper" :total="total"
		   :page-sizes="[10,20,30,40,50]" :pager-count="11"
		   size="default"  background  class="mt-4" :current-page="currentPage"
		   @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
		</div>
		<el-dialog v-model="selectAssignUserDialog" title="选择用户" width="50%">
			<AssignUser :roleId="roleId"  @closeAssignUserForm="closeAssignUserForm" @success="success"></AssignUser>
		</el-dialog>
	</div>
</template>

<script setup lang="ts">
	import {ref,reactive,onMounted} from 'vue'
	import AssignUser from './AssignUser.vue'
	import {ElMessage} from 'element-plus'
	import {assignUserPage} from '@/api/user.ts'
	import {useRouteStore} from '@/stores/route.ts'
	import {unAssignUsers} from '@/api/role.ts'
	import {useUserStore} from '@/stores/user.ts'
	const userStore = useUserStore()
	
	const useRoute = useRouteStore();
	const route =  useRoute.route;
	const roleId = route.params.roleId;
	console.log("roleId:"+roleId);
	const cancelAssignUser = async (id:number) => {
	  if(selections.value.length < 0){
	  	ElMessage({
	  	  type: 'warning',
	  	  message: '至少选择一行进行删除',
	  	})
	  	return;
	  }
	  let ids = [];
	  if(id != undefined){
	  	ids.push(id);
	  }else{
	  	selections.value.forEach((item)=>{
	  		ids.push(item.id)
	  	})
	  }
	  console.log("ids:"+ids)
	  await unAssignUsers(ids)
	  loadData();
	}
	
	let searchParam =reactive({
		username:null,
		phone:null,
		roleId:roleId,
		page:1,
		limit:10
	})
	const search=()=>{
		loadData();
	}
	const queryFormRef=ref<FormInstance>();
	const reset = (formEl: FormInstance | undefined) => {
		  if (!formEl) return
		  formEl.resetFields()
		  loadData();
	}
	
	const selections = ref([])
	const handleSelectionChange=(val)=>{
		selections.value=val;
	}
	
	
	onMounted(()=>{
		loadData();
	})
	const total=ref(0);
	const loading=ref(false)
	const tableData = ref([]);
	const loadData=()=>{
		loading.value=true;
		assignUserPage(searchParam).then((res)=>{
			if(res.code == 200){
				tableData.value=res.data.list;
				total.value=res.data.total
			}
			loading.value=false;
		})
	}
	
	const currentPage = ref(1)
	const handleSizeChange = (val: number) => {
	  console.log(`${val} items per page`)
	  searchParam.limit= val;
	  loadData();
	}
	const handleCurrentChange = (val: number) => {
	  console.log(`current page: ${val}`)
	  currentPage.value= val;
	  searchParam.page= val;
	  loadData();
	}
	
	const selectAssignUserDialog = ref(false);
	
	// 关闭保存用户弹出框
	const closeAssignUserForm = ()=> {
	  selectAssignUserDialog.value = false
	}
	
	// 提交表单回调函数
	const success = ()=> {
	  selectAssignUserDialog.value = false
	  loadData();
	}
	const userInfo = ref()
	const addAssignUser=()=>{
		selectAssignUserDialog.value = true;
	}
	const close=()=>{
		window.history.back()
	}
</script>

<style scoped>
	.search{
		margin-bottom: 10px;
	}
  .el-pager{
    padding: 2%;
    justify-content: flex-start;
    align-items: flex-start;
    position: absolute;
    bottom:0;
    right:0;
    margin-bottom:25px;
    width: 100%;
    background-color: white;
    z-index: 1;
  }
	.allcontent{
		height: 80vh;
		position: relative;
	}
	.el-table{
		max-height:70%;
		overflow: auto;
    z-index: 0;
	}
	.el-table::-webkit-scrollbar{
		display: none;
	}
</style>